using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.SupportingMaterials
{
    public partial class HSupportingMaterials : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new HSupportingMaterialFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as HSupportingMaterialFilter;
            if (filter != null) filter.IncludeInactive = CheckBoxInactive.Checked;
            GridViewSupportingMaterials.DataBind();
        }

        /// <summary>
        /// Objects the data source supporting material selecting.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ObjectDataSourceSupportingMaterialSelecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as HSupportingMaterialFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonUrl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonUrl_Command(object sender, CommandEventArgs e)
        {
            int supportingMaterialId = Convert.ToInt32(e.CommandArgument, CultureInfo.CurrentCulture);
            SupportingMaterial supportingMaterial = HSupportingMaterialBO.GetSupportingMaterialById(supportingMaterialId);

            if (supportingMaterial.IsActive)
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Organization/HEditSupportingMaterial.aspx?id={0}",
                                                supportingMaterialId));
            }
            else
            {
                var linkButton = ((LinkButton)sender);
                linkButton.OnClientClick = "return confirm('Do you want to make this Supporting Materials active?')";
            }
        }
    }
}